Imports System.Windows.Forms

Public Class DlgColorSetting
    Private labels As List(Of Label)
    Private txts As List(Of TextBox)
    Private btns As List(Of Button)
    Private colorCtr As Integer = 1

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        SaveColor()
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim btn As Button = CType(sender, Button)
        If btn IsNot Nothing Then
            If btns.Contains(btn) Then
                Dim btnIndex As Integer = btns.IndexOf(btn)
                If btnIndex < labels.Count Then
                    dlgColor.Color = labels(btnIndex).BackColor
                    If dlgColor.ShowDialog() = Windows.Forms.DialogResult.OK Then
                        labels(btnIndex).BackColor = dlgColor.Color
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub DlgColorSetting_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        labels = New List(Of Label)()
        txts = New List(Of TextBox)()
        btns = New List(Of Button)()
        If ColorSetting IsNot Nothing Then
            For Each tmpColor As Object In ColorSetting.Keys
                AddNewRow(tmpColor)
            Next
        End If
    End Sub

    Private Sub AddNewRow(ByVal colorName As Object, Optional ByVal colorCode As Integer = 0)
        Dim newTxtBox As New TextBox()
        newTxtBox.BorderStyle = BorderStyle.FixedSingle
        newTxtBox.Text = Convert.ToString(colorName)
        txts.Add(newTxtBox)

        Dim newLabel As New Label()
        newLabel.BorderStyle = BorderStyle.FixedSingle
        newLabel.Anchor = (AnchorStyles.Left Or AnchorStyles.Right)
        If colorCode = 0 Then
            newLabel.BackColor = Color.FromArgb(Convert.ToInt32(ColorSetting(colorName)))
        Else
            newLabel.BackColor = Color.FromArgb(colorCode)
        End If
        labels.Add(newLabel)

        Dim newBtn As New Button()
        newBtn.Image = Global.LogViewer.My.Resources.Resources.openicon
        btns.Add(newBtn)
        AddHandler newBtn.Click, AddressOf btn_Click

        If colorCtr > tableLyt.RowCount Then tableLyt.RowCount = colorCtr
        tableLyt.Controls.Add(newTxtBox, 0, tableLyt.RowCount - 1)
        tableLyt.Controls.Add(newLabel, 1, tableLyt.RowCount - 1)
        tableLyt.Controls.Add(newBtn, 2, tableLyt.RowCount - 1)
        colorCtr = colorCtr + 1

    End Sub

    Private Sub SaveColor()
        Dim ctr As Integer = 0
        While ctr < colorCtr
            If ctr < txts.Count And ctr < labels.Count Then
                ColorSetting(txts(ctr).Text) = labels(ctr).BackColor.ToArgb()
            End If
            ctr = ctr + 1
        End While
    End Sub

    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
        AddNewRow("New Color", Color.Navy.ToArgb())
    End Sub
End Class
